#include "mainwindow.h"
#include <QAction>
#include <QMessageBox>
#include <QToolBar>
#include <QMenuBar>
#include <Qt>
#include <QDebug>
#include <QInputDialog>

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("Main Window"));

    openAction = new QAction(QIcon(":/images/doc-open"), tr("&Open..."), this);
    openAction->setShortcuts(QKeySequence::Open);
    openAction->setStatusTip(tr("Open an existing file"));
    connect(openAction, &QAction::triggered, this, &MainWindow::open);

    QMenu* file = menuBar()->addMenu(tr("&File"));
    file->addAction(openAction);

    QToolBar* toolBar = addToolBar(tr("&File"));
    toolBar->addAction(openAction);

    openActionTwo = new QAction(QIcon(":/images/doc-open"), tr("&OpenTwo..."), this);
    connect(openActionTwo, &QAction::triggered, this, &MainWindow::openTwo);

    QToolBar* toolBarTwo = addToolBar(tr("&FileTwo"));
    toolBarTwo->addAction(openActionTwo);

    statusBar();
}

MainWindow::~MainWindow()
{
}

void MainWindow::open(void)
{
    //    QMessageBox::information(this, tr("Information"), tr("Open"));
    QDialog* d = new QDialog(this);
    d->setAttribute(Qt::WA_DeleteOnClose);
    d->setWindowTitle(tr("Hello, dialog!"));
    d->exec(); // application-level modal dialogs
    //    d->open(); // window-level modal dialogs
    //    d->show(); // non-modal dialogs
    qDebug() << d->result();
}

void MainWindow::openTwo(void)
{
    //    QInputDialog* qid = new QInputDialog(this);
    //    qid->exec();
    //    qDebug() << qid->textValue();

    //    if (QMessageBox::Yes == QMessageBox::question(this,
    //                                                  tr("Question"),
    //                                                  tr("Are you OK?"),
    //                                                  QMessageBox::Yes | QMessageBox::No,
    //                                                  QMessageBox::Yes)) {
    //        QMessageBox::information(this, tr("Hmmm..."), tr("I'm glad to hear that!"));
    //    } else {
    //        QMessageBox::information(this, tr("Hmmm..."), tr("I'm sorry!"));
    //    }

    QMessageBox msgBox;
    msgBox.setText(tr("The document has been modified."));
    msgBox.setInformativeText(tr("Do you want to save your changes?"));
    msgBox.setDetailedText(tr("Differences here..."));
    msgBox.setStandardButtons(QMessageBox::Save
                              | QMessageBox::Discard
                              | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();
    switch (ret) {
    case QMessageBox::Save:
        qDebug() << "Save document!";
        break;
    case QMessageBox::Discard:
        qDebug() << "Discard changes!";
        break;
    case QMessageBox::Cancel:
        qDebug() << "Close document!";
        break;
    }
    qDebug() << "rst: 0x" << QString::number(msgBox.result(), 16).toInt();
}
